package com.xydtech.ecard.config.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.domain.EcardBusiReportCard;
import com.xydtech.ecard.config.mapper.EcardBusiAndConfigMapper;
import com.xydtech.ecard.config.mapper.EcardBusiReportCardMapper;
import com.xydtech.ecard.config.service.IEcardBusiReportCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子名片业务填报绑卡类信息Service业务层处理
 *
 * @author xydtech
 * @date 2024-09-29
 */
@Service
public class EcardBusiReportCardServiceImpl implements IEcardBusiReportCardService
{
    @Autowired
    private EcardBusiReportCardMapper ecardBusiReportCardMapper;
    @Autowired
    private EcardBusiAndConfigMapper ecardBusiAndConfigMapper;


    /**
     * 查询电子名片业务填报绑卡类信息
     *
     * @param id 电子名片业务填报绑卡类信息主键
     * @return 电子名片业务填报绑卡类信息
     */
    @Override
    public EcardBusiReportCard selectEcardBusiReportCardById(Long id, String salt)
    {
        EcardBusiReportCard ecardBusiReportCard = ecardBusiReportCardMapper.selectEcardBusiReportCardById(id);
        if (ecardBusiReportCard != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustPhone())) {
                    ecardBusiReportCard.setPerCustPhone(AESEncryptionUtils.decrypt(ecardBusiReportCard.getPerCustPhone(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustPhone())) {
                    ecardBusiReportCard.setPerCustIdcard(AESEncryptionUtils.decrypt(ecardBusiReportCard.getPerCustPhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return ecardBusiReportCard;
    }

    /**
     * 查询电子名片业务填报绑卡类信息列表
     *
     * @param ecardBusiReportCard 电子名片业务填报绑卡类信息
     * @return 电子名片业务填报绑卡类信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<EcardBusiReportCard> selectEcardBusiReportCardList(EcardBusiReportCard ecardBusiReportCard, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustPhone())) {
                ecardBusiReportCard.setPerCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportCard.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustIdcard())) {
                ecardBusiReportCard.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportCard.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<EcardBusiReportCard> ecardBusiReportCards = ecardBusiReportCardMapper.selectEcardBusiReportCardList(ecardBusiReportCard);
        if (ecardBusiReportCards.size() > 0) {
            for (EcardBusiReportCard busiReportCard : ecardBusiReportCards) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReportCard.getPerCustPhone())) {
                        busiReportCard.setPerCustPhone(AESEncryptionUtils.decrypt(busiReportCard.getPerCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReportCard.getPerCustIdcard())) {
                        busiReportCard.setPerCustIdcard(AESEncryptionUtils.decrypt(busiReportCard.getPerCustIdcard(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return ecardBusiReportCards;
    }

    /**
     * 新增电子名片业务填报绑卡类信息
     *
     * @param ecardBusiReportCard 电子名片业务填报绑卡类信息
     * @return 结果
     */
    @Override
    public int insertEcardBusiReportCard(EcardBusiReportCard ecardBusiReportCard, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustPhone())) {
                ecardBusiReportCard.setPerCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportCard.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustIdcard())) {
                ecardBusiReportCard.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportCard.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReportCardMapper.insertEcardBusiReportCard(ecardBusiReportCard);
    }

    /**
     * 修改电子名片业务填报绑卡类信息
     *
     * @param ecardBusiReportCard 电子名片业务填报绑卡类信息
     * @return 结果
     */
    @Override
    public int updateEcardBusiReportCard(EcardBusiReportCard ecardBusiReportCard, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustPhone())) {
                ecardBusiReportCard.setPerCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportCard.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportCard.getPerCustIdcard())) {
                ecardBusiReportCard.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportCard.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReportCardMapper.updateEcardBusiReportCard(ecardBusiReportCard);
    }

    /**
     * 批量删除电子名片业务填报绑卡类信息
     *
     * @param ids 需要删除的电子名片业务填报绑卡类信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportCardByIds(String ids)
    {
        //循环删除电子名片业务填报信息和业务类型关联表
        for (String busiId : ids.split(",")) {
            ecardBusiAndConfigMapper.deleteEcardBusiAndConfigByConfigIdAndBusiType(busiId, "2");
        }
        return ecardBusiReportCardMapper.deleteEcardBusiReportCardByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片业务填报绑卡类信息信息
     *
     * @param id 电子名片业务填报绑卡类信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportCardById(Long id)
    {
        return ecardBusiReportCardMapper.deleteEcardBusiReportCardById(id);
    }
}
